package org.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

/**
 * <h3>总结:</h3>
 * <ul>
 * <li>使用ModelAndView返回页面</li>
 * <li>
 * <ul>
 * <li>如果是forward方式,则不需要加ContextPath</li>
 * <li>如果是redirect方式,则必须加ContextPath</li>
 * </ul>
 * </li>
 * <li>直接使用"forward:/{页面}"返回页面,不论是forward还是redirect都不加ContextPath</li>
 * <li>不论方法的返回类型是ModelAndView还是String,可以直接写页面路径,跳转方式为forward</li>
 * <li>但是写了forward关键字后就需要从/(根)开始写</li>
 * </ul>
 * @author Garfield
 */
@Controller
public class IndexController {

    /*************************跳转结束*************************/

    /**
     * 控制层跳转,服务器跳转
     * @return
     */
    @RequestMapping(value = "/error_index")
    public ModelAndView error_index() {
        System.out.println("new ModelAndView(\"forward:/admin/index/index\")实现了控制层跳转！");
        return new ModelAndView("forward:/admin/index");
    }

    /**
     * 跳转页面,服务器跳转
     * @return
     * @throws InterruptedException
     */
    @RequestMapping(value = "/index_")
    public ModelAndView index_() {
        return new ModelAndView("forward:/page/login.jsp");
    }

    /**
     * 跳转页面,服务器跳转
     * @return
     */
    @RequestMapping(value = "/index_s")
    public ModelAndView index_s() {
        return new ModelAndView("login");
    }

    /**
     * 跳转页面,服务器跳转
     * @return
     */
    @RequestMapping(value = "/index")
    public String index() {
        return "login";
    }

    /**
     * 跳转页面,服务器跳转
     * @return
     */
    @RequestMapping(value = "/index_forward")
    public String index_forward() {
        return "forward:/page/login.jsp";
    }

    /**********************跳转结束,重定向开始**********************/

    /**
     * 重定向(需从app根目录开始)
     * @param request
     * @return
     */
    @RequestMapping(value = "/login")
    public ModelAndView login(HttpServletRequest request) {
        String path = request.getContextPath();
        return new ModelAndView(new RedirectView(path + "/page/login.jsp"));
    }

    /**
     * 重定向(需从app根目录开始)
     * @return
     */
    @RequestMapping(value = "/login_s")
    public String login_s() {
        return "redirect:/page/login.jsp";
    }
    /************************重定向结束************************/


    /**
     * 欢迎界面
     * @return
     */
    @RequestMapping(value = "/welcome")
    public String welcome() {
        return "redirect:/page/welcome.jsp";
    }

    /**
     * 错误界面
     * @return
     */
    @RequestMapping(value = "/error")
    public String error() {
        return "redirect:/page/error/error.jsp";
    }

    /**
     * spring异常拦截测试
     * @throws Exception
     */
    @RequestMapping(value = "/exception")
    public void exception() throws Exception {
        throw new Exception();
    }
}
